package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.pojo.Num;
import com.bwie.pojo.TbPrize;
import com.bwie.pojo.User;
import com.bwie.service.impl.TbNumServiceImpl;
import com.bwie.service.impl.TbUserServiceImpl;
import com.bwie.service.impl.TbprizeServiceImpl;
import com.bwie.service.impl.TokenImpl;
import com.bwie.utils.R;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/getnum/pintuan")
@Slf4j
public class Controller {

    private static final Logger logger = LoggerFactory.getLogger(Controller.class);

    @Autowired
    TbNumServiceImpl tbNumService;

    @Autowired
    TbUserServiceImpl tbUserService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    TbprizeServiceImpl tbprizeService;

    @Autowired
    TokenImpl token;

    /**
     * 获取历史记录
     * @param num
     * @return
     */
    @PostMapping("/getnum")
    public Num getnum(@Validated @RequestBody Num num){
        LambdaQueryWrapper<Num> numLambdaQueryWrapper = new LambdaQueryWrapper<>();
        numLambdaQueryWrapper.eq(Num::getTgId,num.getTgId());
        Num one = tbNumService.getOne(numLambdaQueryWrapper);
        return one;
    }

    public static final String code = "123456";

    /**
     * 登录获取token
     */
    @PostMapping("/login")
    public R gettoken(@RequestBody User user){
        String userIdLI = token.gettoken(user);
        return R.OK(userIdLI);
    }

    /**
     * 解析.检查token
     */
    @PostMapping("/checktoken")
    public R checktoken(@RequestBody User user){
        String userIdLI = token.checktoken(user);
        return R.OK(userIdLI);
    }

    /**
     * 抽奖
     */
    @GetMapping("/lottery")
    public R Lottery(){
        String id = "1";
        R lottery = Lottery(id);
        return lottery;
    }

    /**
     * 抽奖
     */
    Integer max = 1;
    public R Lottery(@PathVariable("id")String id){
        TbPrize byId = tbprizeService.getById(id);
        Integer prizeNum = Integer.valueOf(byId.getPrizeNum());
        //判断是否重新抽奖
        if (prizeNum<=0 && max>0){
            max--;
            Lottery();
            log.error("自动重新抽奖一次");
        }
        //判断库存为0
        if (prizeNum<=0){
            return R.ERROR(444,"奖池好像有点空");
        }
        max = 3;
        //删除库存数量
        byId.setPrizeNum(String.valueOf(prizeNum-1));
        tbprizeService.updateById(byId);
        return R.OK("奖品为:"+byId.getPrizeValue());
    }

    /**
     * 抽奖库存列表
     */
    @GetMapping("/getprizelist")
    public R GetPrizeList(){
        List<TbPrize> list = tbprizeService.list();
        return R.OK(list);
    }

    /**
     * 抽奖库存新增/修改
     */
    @PostMapping("/addpirze")
    public R AddPrize(@RequestBody TbPrize tbPrize){
        if ("".equals(tbPrize.getPrizeValue())){
            return R.ERROR(234,"奖品内容不能为空");
        }
        if (!"".equals(tbPrize.getPrizeId())&&tbPrize.getPrizeId()!=null){
            tbprizeService.updateById(tbPrize);
        }else {
            tbprizeService.save(tbPrize);
        }
        return R.OK();
    }

}
